//
//  HQRetailersDetailViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/8.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQRetailersDetailViewController.h"

@interface HQRetailersDetailViewController ()
{
    NSDictionary *_currentDict;
    NSString *_price;
    __weak IBOutlet UILabel *_wNum2;
}
@end

@implementation HQRetailersDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"产品详情";
    
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemTitle:@"管理" withTitleColor:[self colorHexValue:0x647180] withFont:16 withTarget:self withSelector:@selector(rightItem)];
    
    [_xsmsField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
    [self requestProductDetail];
}

-(void)textChange{
    _sjxsPrice.text = [NSString stringWithFormat:@"%.2f",[_xsmsField.text floatValue] * [_price floatValue]];
}

/**
 产品详情管理
 */
-(void)rightItem{
    [self tostView:@"管理商品"];
}

/**
 请求店内商品详情
 */
-(void)requestProductDetail{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam worksFindWorksDetailsWithWID:self.dataInfo[@"w_id"]] withMethod:[HQMethod worksFindWorksDetails] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSDictionary *dic = sucData[@"data"];
            _currentDict = dic;
            NSLog(@"%@",dic);
            // 产品图片
            [_wImg setImageWithURL:[NSURL URLWithString:dic[@"w_img"]]];
            // 产品编号
            _wStyle.text = [NSString stringWithFormat:@" %@ ",dic[@"w_num"]];
            // 剩余数量
            _wNum2.text = [NSString stringWithFormat:@"剩余数量 %@",dic[@"w_kucun"]];
            // 产品风格
            [self sourceLabel:_wNum withTitle:@"产品风格: " withData:dic[@"w_style"]];
            // 产品成分
            [self sourceLabel:_wElement withTitle:@"产品成分: " withData:dic[@"w_element"]];
            // 生产厂家
            [self sourceLabel:_wFactory withTitle:@"生产厂家: " withData:dic[@"w_factory"]];
            // 设计理念
            [self sourceLabel:_wConcept withTitle:@"设计理念: " withData:dic[@"w_concept"]];
            // 产品价格
            [self sourceLabel:_wPrice withTitle:@"￥" withData:dic[@"w_price"] withData2:@""];
            
            _price = dic[@"w_price"];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 查看大图
 
 @param sender 按钮
 */
- (IBAction)lookBigImage:(id)sender {
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = self.dataInfo[@"w_img"];
    [self presentViewController:bigImageVc animated:YES completion:nil];
}

/**
 出售商品
 
 @param sender 按钮
 */
- (IBAction)saleItem:(id)sender {
    if([self isInputEmpty:_xsmsField.text]){
        [self tostView:@"请输入销售米数"];
        return;
    }else if([self isInputEmpty:_sjxsPrice.text]){
        [self tostView:@"请输入商品实际销售价格"];
        return;
    }else if([self isInputEmpty:_gkxmField.text]){
        [self tostView:@"请输入顾客姓名"];
        return;
    }else if([self isInputEmpty:_sjhmField.text]){
        [self tostView:@"请输入手机号码"];
        return;
    }else if(![HQTool phoneNoRegex:_sjhmField.text]){
        [self tostView:@"请输入正确的手机号码"];
        return;
    }
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam save_workorder:[UserModelManager userID] name:_gkxmField.text phone:_sjhmField.text s_id:self.dataInfo[@"w_id"] danjia:[NSString stringWithFormat:@"%@",_currentDict[@"w_price"]]  yongliang:_xsmsField.text price:_sjxsPrice.text] withMethod:[HQMethod save_workorder] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSDictionary *dic = sucData[@"data"];
            NSLog(@"%@",dic);
            NSString *infoStr = [NSString stringWithFormat:@"%@,请到订单记录界面查看!",sucData[@"info"]];
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:infoStr  preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
            }]];
            [self presentViewController:alertController animated:YES completion:nil];
            
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
